<template>
  <div class="login-wrap">
    <el-card class="card-wrap" body-style="padding-left: 0; padding-right: 0; padding-top: 20px">
      <el-form :model="form" class="form-wrap" ref="ruleFormRef" :rules="rules">
        <el-form-item>
          <div class="input-item from-title">
            <span>VM-TASK</span>
          </div>
        </el-form-item>
        <el-form-item prop="username">
          <el-input class="input-item" size="large" :prefix-icon="User" placeholder="请输入用户名" v-model="form.username" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input class="input-item item-margin" type="password" size="large" show-password v-model="form.password" :prefix-icon="Lock" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="input-item item-margin" size="large" :loading="btnLoading" @click="handleLogin(ruleFormRef)">登 录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { LoginData } from '@/data/AuthData.ts'
import { login } from '@/api/AuthApi.ts'
import { storeToRefs } from 'pinia'
import { tokenStore, userStore } from '@/store/userStore'
import { FormInstance, FormRules } from 'element-plus'

const form = ref<LoginData>({
  username:'',
  password:''
})

const router = useRouter()
const { token } = storeToRefs(tokenStore())
const userStoreData = userStore()
const logoUrl = ref<string>(new URL('../assets/logo.png', import.meta.url).href)
const btnLoading = ref<boolean>(false)
const ruleFormRef = ref<FormInstance>()

const rules = reactive<FormRules<LoginData>>({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 4, max: 20, message: '长度为4-20位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 20, message: '长度为6-20位', trigger: 'blur' },
  ],
})

const handleLogin = async (formEl: FormInstance | undefined) => {
  if(!formEl){
    return
  }
  await formEl.validate(async (valid) => {
    if(valid){
      btnLoading.value = true
      const res = await login(form.value)
      btnLoading.value = false
      if(res.data){
        token.value = res.data
        userStoreData.setUserInfo({
          username: form.value.username,
          avatar: logoUrl.value
        })
        await router.push({path: '/'})
      }
    }
  })
}

</script>

<style lang="less" scoped>
.login-wrap{
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;

  .card-wrap{
    border-radius: 15px;
  }

  .input-item{
    width: 280px;
  }

  .item-margin{
    margin-top: 10px;
  }

  .form-wrap{
    padding-left: 25px;
    padding-right: 25px;
  }

  .from-title{
    text-align: center;
    font-size: 20px;
    color: #3f9eff;
  }
}
</style>